<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

abstract class Widget extends Base {
	// 视图实例对象
    protected $view   =  null;

	public function __construct()
    {
        //实例化视图类
        $this->view = View::getInstance();
	}

    // 渲染输出
    // data 要渲染的数据
    abstract public function render($data=array());

	public function assign($name,$value='')
    {
        $this->view->assign($name,$value);
    }

    // 渲染模板输出
    protected function renderFile($templateFile='',$charset='',$contentType='text/html',$varPrefix='')
	{
        if(!file_exists_case($templateFile)){
            // 自动定位模板文件
            $filename   =  empty($templateFile)?substr(get_class($this),0,-6):$templateFile;
            $templateFile = LIB_PATH.'Widget/'.$filename.C('TEMPLATE_SUFFIX');
            if(!file_exists_case($templateFile)){
                throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
            }
        }
        $content = $this->view->fetch($templateFile,$charset,$contentType,$varPrefix,false);
        return $content;
    }
}
?>